<?php
class TinyBrick_OrderEdit_Block_Adminhtml_Sales_Order_View_Info extends Mage_Adminhtml_Block_Sales_Order_View_Info
{
    protected $_countryCollection;
	protected function _toHtml()
    {
    	$str = Mage::app()->getFrontController()->getRequest()->getPathInfo();
    	if(strpos($str, '/sales_order/view/')) {
    		$this->setTemplate('sales/order/view/edit.phtml');
    	}
        if (!$this->getTemplate()) {
            return '';
        }
        $html = $this->renderView();
        return $html;
    }
    
    public function getCountryList()
    {
		return Mage::getResourceModel('directory/country_collection')
				->addFieldToFilter('country_id', array('in' => explode(",", Mage::getStoreConfig('general/country/allow'))))
				->toOptionArray();
    }
    
    public function getStateList()
    {
    	$states = Mage::getResourceModel('directory/region_collection')
    			->addFieldToFilter('country_id', array('in' => explode(",", Mage::getStoreConfig('general/country/allow'))))
    			->setOrder('country_id', 'DESC')
    			->setOrder('default_name', 'ASC')
    			->load();
		$states = $states->getData();
		return $states;
    }
    public function getCountryHtmlSelect($type)
    {
        if($type=='billing'){
            $countryId = $this->getOrder()->getBillingAddress()->getCountryId();
        }else{
            $countryId = $this->getOrder()->getShippingAddress()->getCountryId();
        }
        if (is_null($countryId)) {
            $countryId = Mage::helper('core')->getDefaultCountry();
        }
        $select = $this->getLayout()->createBlock('core/html_select')
            ->setName('country_id')
            ->setId($type.':country_id')
            ->setTitle(Mage::helper('sales')->__('Country'))
            ->setClass('validate-select')
            ->setValue($countryId)
            ->setOptions($this->getCountryOptions());
        if ($type === 'shipping') {
            $select->setExtraParams('onchange="shipping.setSameAsBilling(false);"');
        }

        return $select->getHtml();
    }
    public function getCountryOptions()
    {
        $options    = false;
        $useCache   = Mage::app()->useCache('config');
        if ($useCache) {
            $cacheId    = 'DIRECTORY_COUNTRY_SELECT_STORE_' . Mage::app()->getStore()->getCode();
            $cacheTags  = array('config');
            if ($optionsCache = Mage::app()->loadCache($cacheId)) {
                $options = unserialize($optionsCache);
            }
        }

        if ($options == false) {
            $options = $this->getCountryCollection()->toOptionArray();
            if ($useCache) {
                Mage::app()->saveCache(serialize($options), $cacheId, $cacheTags);
            }
        }
        return $options;
    }
    public function getCountryCollection()
    {
        if (!$this->_countryCollection) {
            $this->_countryCollection = Mage::getSingleton('directory/country')->getResourceCollection()
                ->loadByStore();
        }
        return $this->_countryCollection;
    }
}
